/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	Dotnetnuke PA module 
	Copyright (c) 2004, Oliver Hine (oliver.hine@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.VisualBasic;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.BusinessRules;
using Votations.NSurvey.WebAdmin.UserControls;
using Votations.NSurvey.WebControls.UI;
using Votations.NSurvey.WebControlsFactories;
using Votations.NSurvey.Web;

namespace Votations.NSurvey.WebAdmin
{
	public class LibraryTemplates: PageBase
	{
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.InsertQuestionButton.Click += new System.EventHandler(this.InsertQuestionButton_Click);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		protected System.Web.UI.WebControls.Label MessageLabel;
		protected HeaderControl Header;
		protected System.Web.UI.WebControls.Literal LibraryQuestionsTemplatesTitle;
		protected System.Web.UI.WebControls.PlaceHolder TemplatesPlaceHolder;
		protected System.Web.UI.WebControls.Button InsertQuestionButton;

		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				SetupSecurity();
				MessageLabel.Visible = false;

				_libraryId = 
					Information.IsNumeric(Request["LibraryId"]) ? int.Parse(Request["LibraryId"]) : -1;
	
				LocalizePage();
				BuildTemplateList();
	
				if (!Page.IsPostBack)
				{
					Header.SurveyId = SurveyId;
				}
			}
			catch( Exception exc)
			{
				DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this,exc);
			}
		}

		private void SetupSecurity()
		{
			CheckRight(NSurveyRights.AccessLibrary, true);
			InsertQuestionButton.Visible = NSurveyUser.Identity.IsAdmin || CheckRight(NSurveyRights.ManageLibrary, false);
		}

		private void LocalizePage()
		{
			LibraryQuestionsTemplatesTitle.Text = GetPageResource("QuestionTemplatesTitle");
		}


		private void BuildTemplateList()
		{
			_questionsData = new Questions().GetLibraryQuestions(_libraryId);	
			
			foreach (QuestionData.QuestionsRow question in _questionsData.Questions.Rows)
			{
				Table questionsContainer = new Table();
				questionsContainer.Width = Unit.Percentage(100);
				Table questionTable = new Table();
				questionTable.CellSpacing = 2;
				questionTable.CellPadding = 4;
				questionTable.CssClass = "questionBuilder";
				AddQuestionWebControl(questionTable, question);
				questionsContainer.Rows.Add(BuildRow(questionTable));
				TemplatesPlaceHolder.Controls.Add(questionsContainer);
			}

		}


		private TableRow BuildRow(Control child)
		{
			TableRow row = new TableRow();
			TableCell cell  = new TableCell();
			cell.Controls.Add(child);
			row.Cells.Add(cell);
			return row;
		}

		/// <summary>
		/// Get a web control instance of the question row 
		/// and adds it with its options to the table 
		/// </summary>
		private void AddQuestionWebControl(Table questionTable, QuestionData.QuestionsRow question)
		{
			QuestionItem questionWebControl = QuestionItemFactory.Create(question, this.UniqueID, 0, null, true);

			// Set question's style
			// and bind the data
			Style questionStyle = new Style();
			questionStyle.CssClass = "mediumText";
			Style answerStyle = new Style();
			answerStyle.CssClass = "surveyAnswer";	
			Style markStyle = new Style();

			if (questionWebControl is ActiveQuestion)
			{
				((ActiveQuestion)questionWebControl).EnableClientSideValidation = false;
				((ActiveQuestion)questionWebControl).EnableServerSideValidation = false;
			}
			if (questionWebControl is MatrixQuestion)
			{
				((MatrixQuestion)questionWebControl).MatrixHeaderStyle = answerStyle;
				((MatrixQuestion)questionWebControl).MatrixItemStyle = answerStyle;
				((MatrixQuestion)questionWebControl).MatrixAlternatingItemStyle = answerStyle;
			}

			questionWebControl.AnswerStyle = answerStyle;
			questionWebControl.QuestionStyle = questionStyle;
			
			
			questionWebControl.DataBind();
						
			// Add the question and its options to the table
			if (NSurveyUser.Identity.IsAdmin || CheckRight(NSurveyRights.ManageLibrary, false))
			{
				questionTable.Rows.Add(BuildQuestionOptionsRow(question));
			}
			questionTable.Rows.Add(BuildRow(questionWebControl));
		}

		private TableRow BuildQuestionOptionsRow(QuestionData.QuestionsRow question)
		{
			LibraryQuestionOptionsControl libraryQuestionOptionsControl = 
				(LibraryQuestionOptionsControl)LoadControl("UserControls/LibraryQuestionOptionsControl.ascx");
			libraryQuestionOptionsControl.Question = question;

			return BuildRow(libraryQuestionOptionsControl);
		}

		private void InsertQuestionButton_Click(object sender, System.EventArgs e)
		{
			UINavigator.NavigateToInsertQuestion(SurveyId, 1, 1, _libraryId, MenuIndex);
		}

		int _libraryId = -1;
		QuestionData _questionsData;

	}
}
